/**
 * Created by CJB on 2017-01-03 10:52:25.
 */

var loadDiv = null;
	$(function () {
		loadDiv = $("#loadDiv").loading();//初始化提示操作执行中
    initAddWindow();
    //去除所有控件的虚线框
	removeBlur();
});

/**
 * 初始化添加窗口
 */
function initAddWindow(){
    var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
    zTree = zTree.setting.callback.onClick = onDialogTreeOnClick("authorityAddList","parentId");
    //声明消息提示
    var toolTips = new Object();
    //设置表单验证
    var validator = $("#addForm").validate({
        rules:{
            name:"required",
            permission:"required",
            authorityAddList:"required"
        },
        messages:{
            name:"请输入权限名称",
            permission:"请输入权限标识",
            authorityAddList:"请选择父节点"
        },
        errorPlacement: function(eror,element){
            var toolTip = createTooltip(toolTips,element.attr("id"));
            toolTip.setContent(eror.text());
            element.addClass("error-border");

        },
        success:function(label){
            var elementId = label.attr("id").substring(0,label.attr("id").length - 6);
            var toolTip = toolTips[elementId];
            $("#" + elementId).removeClass("error-border");
            toolTip.close();
            toolTip.detach();
        },
        submitHandler:function(form){
        	loadDiv.showLoading();
            post(getPath() + "base/authority/add",
                $("#addForm").serialize(),
                function(resultObject){
            		loadDiv.hideLoading();
                    var errors = resultObject.errors;
                    //判断后台是否有返回错误消息
                    if(errors && errors.length > 0){
                        for(var i=0; i < errors.length; i++){
                            var error = errors[i];
                            var toolTip = createTooltip(toolTips,error.field);
                            toolTip.setContent(error.defaultMessage);
                            $("#" + error.field).addClass("error-border");
                        }
                    }else{
                        var item = resultObject.item;
                        var parentNode = getNodeById('parentMenuTree', item.parentId);
                        var menuparentNode = getNodeById('menuTree', item.parentId);
                        if(parentNode){
                            var newNode = {};
                            newNode.id = item.id;
                            newNode.pId = item.parentId;
                            newNode.name = item.name;
                            newNode.title = item.name;
                            var zTree = $.fn.zTree.getZTreeObj('parentMenuTree');
                            zTree.addNodes(parentNode, newNode);
                            var zTree = $.fn.zTree.getZTreeObj('menuTree');
                            zTree.addNodes(menuparentNode, newNode);
                            zTree.expandNode(menuparentNode,true);
                        }
                        $(".grid-list").html("");
                        //添加成功，弹出添加成功提示框，当用户点击确定后刷新页面
                        createWindow("添加成功");
                        $(".grid-list").load(getPath() + "base/authority/add/"+0);
                    }
                },function(resultObject){
	        		loadDiv.hideLoading();
	        		createWindow(resultObject).dialog('open');
	        	}
            )
        }
    });

    $("#authorityAddList").click({menuContentId:"parentMenu"}, showMenu);
}


$("#refreshCacheBtn").click(function(){
	createWindow('确定要刷新缓存？',{
        cancleEnable:true,
        callBack:function(){
        	loadDiv.showLoading();
        	post(getPath()+"base/authority/refreshAuthorityCache",{},function(result){
        		loadDiv.hideLoading();
        		createWindow("缓存刷新成功").dialog("open");
        	});
        }
    }).dialog('open');
});


function heavyLoad(){
	$(".grid-list").load(getPath() + "base/authority/add/"+0);
}